package com.gouqi.mall.service.impl;


import com.gouqi.mall.domain.ProductBrand;
import com.gouqi.mall.domain.ResultJson;
import com.gouqi.mall.mapper.ProductBrandMapper;
import com.gouqi.mall.mapper.SuggeationMapper;
import com.gouqi.mall.service.ProductBrandService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductBrandServiceImpl implements ProductBrandService {

    @Autowired
    private ProductBrandMapper mapper;
    private JSONObject json = new JSONObject();
    private ResultJson result = new ResultJson();

    @Override
    public String addOneBrand(ProductBrand brand) {
        Integer id = mapper.addOneBrand(brand);
        if (id > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("新增品牌成功！");
            result.setResultJson(id);
        } else {
            result.setStatus(404);
            result.setCode(204);
            result.setMessage("新增品牌失败！");
            result.setResultJson("");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String updateOneBrand(ProductBrand brand) {
        Integer id = mapper.updateOneBrand(brand);
        if (id > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("更新品牌成功！");
        } else {
            result.setStatus(404);
            result.setCode(204);
            result.setMessage("更新品牌失败！");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String deleteOneBrand(Integer id) {
        Integer res = mapper.deleteOneBrand(id);
        if (res > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("删除一个品牌成功！");
            result.setResultJson(null);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("删除一个品牌失败！");
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String queryRankBrand(String type) {
        List<ProductBrand> list= mapper.queryRankBrand(type);
        if (list != null) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("查找排名品牌成功！");
            result.setResultJson(list);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("查找排名品牌失败！");
            result.setResultJson(null);
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String queryAllBrands(Integer index,Integer pageSize) {
        List<ProductBrand> list= mapper.queryAllBrands(index,pageSize);
        if (list != null) {
            //统计总品牌数，分页显示
            Integer total = mapper.queryCountBrand();
            //余数
            Integer remainder = total%pageSize;
            //商
            Integer quotient = total/pageSize;
            Integer pageCount =  remainder == 0 ? quotient:quotient+1;
            System.out.println("remainder余数:"+remainder+"  quotient商:"+quotient);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("total",total);
            map.put("index",index);
            map.put("pageSize",pageSize);
            map.put("pageCount",pageCount);
            map.put("productBrandList",list);
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("查找排名品牌成功！");
            result.setResultJson(map);
        } else {
            result.setStatus(404);
            result.setCode(404);
            result.setMessage("查找排名品牌失败！");
            result.setResultJson(null);
        }
        json.put("result",result);
        return json.toString();
    }

    @Override
    public String takeoffOneBrands(Integer id, Boolean takeoff, String takeTime, String takeoffReason) {
        Integer takeoffResult = mapper.takeoffOneBrands(id,takeoff,takeTime,takeoffReason);
        if (takeoffResult > 0) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("下架一个品牌成功！");
            result.setResultJson(null);
        } else {
            result.setStatus(404);
            result.setCode(204);
            result.setMessage("下架一个品牌失败！");
            result.setResultJson(null);
        }
        json.put("result",result);
        return json.toString();
    }

}
